package com.example.stu.kudestu.stu.controller;


import com.example.stu.kudestu.stu.entity.Student;
import com.example.stu.kudestu.stu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    StudentService studentService;

    @PostMapping("/hello")
    private String helloWorld(){
        return "hello";
    }

    @PostMapping("/add")
    public Student save(Student student){
        return studentService.save(student);

    }

    @PostMapping("/update")
    public Student update(Student student){
        return studentService.save(student);
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        studentService.delete(id);
        return "delete success";
    }


    @GetMapping("/findById")
    public Student findById(int id){
        return studentService.findStuById(id);
    }

    @GetMapping("/findByName/{name}")
    public List<Student> findByName(@PathVariable String name){

        return studentService.fingStuByName(name);
}

    @GetMapping("/query")
    public Page<Student> findByPage(Integer page, HttpServletResponse response){

        //解决跨域请求
        response.setHeader("Access-Control-Allow-Origin","*");

        if(page==null || page<=0){
            page = 0;
        }else{
            page -= 1;
        }

        return studentService.findAll(page,5);

    }




}
